package indi.zn.commons.simplepoi;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class SSFBeanRowMatch<T> extends SSFBeanAbstract<T> {
	
		
	public SSFBeanRowMatch(Class<T> clazz,SSFType type) {
		super(clazz,type);
	}

	public SSFBeanRowMatch(Class<T> clazz,SSFType type, Integer start, Integer end,
			String[] RowNumMatchFieldNames) {
		super(clazz,type);
		this.MatchFieldNames = RowNumMatchFieldNames;
		this.start = start;
		this.end = end;

	}

	public Boolean getFormula() {
		return formula;
	}

	public void setFormula(Boolean formula) {
		this.formula = formula;
	}

	public void setMatchFieldNames(String[] match) {

		this.MatchFieldNames = match;
	}

	public void setStatEndNum(Integer start, Integer end) {
		this.start = start;
		this.end = end;
	}

	@SuppressWarnings("rawtypes")
	public List<T> xls2bean(Workbook workbook, String sheetname) throws Exception {


		Sheet sh = workbook.getSheet(sheetname);
		
		if (hfe == null) {
			
			if(type.equals(SSFType.HSSF)) hfe = new  HSSFFormulaEvaluator((HSSFWorkbook)workbook);  
			
			if(type.equals(SSFType.XSSF)) hfe = new  XSSFFormulaEvaluator((XSSFWorkbook)workbook);  
			
		}
		List<T> list = new ArrayList<T>();


		Boolean flag = true;

		for (int i = 0; i < MatchFieldNames.length; i++) {

			if (MatchFieldNames[i] == null || MatchFieldNames[i].equals("")) {
				continue;
			}

			
//			System.out.println(i);
			
			Row row = sh.getRow(i);
			
			
			for (int j = 0; j < end - start; j++) {

				T intance = null;

				if (flag) {

					intance = clazz.newInstance();
					list.add(intance);

				} else {
					intance = list.get(j);
				}

				Field f = clazz.getDeclaredField(MatchFieldNames[i]);

				Class type = f.getType();

				String getMethodname = "set"
						+ MatchFieldNames[i].substring(0, 1).toUpperCase()
						+ MatchFieldNames[i].substring(1);

				Method method = clazz.getMethod(getMethodname, type);

	//			System.out.println(j + start +  "    s:   " + start);
				
				Cell cell = row.getCell(j + start);

				Object value = getValue(cell, hfe, false);

				if (value == null) {
					value = "";
				}

				if (Integer.class.equals(type)) {
					
					if(value instanceof String){
					
					method.invoke(intance,
							Integer.parseInt(value.toString()));
					}
					else if(value instanceof Double){
						
					method.invoke(intance,((Double)value).intValue());
					
					}
					else if(value instanceof Integer){
							
						method.invoke(intance,value);
						
					}
					
					}

				if (String.class.equals(type)) {
					method.invoke(intance, value.toString());
				}

				if (Double.class.equals(type)) {
				
						method.invoke(intance,Double.parseDouble(value.toString()));
				}

			}

			flag = false;

		}
		return list;

	}
	
	
	
	@SuppressWarnings("unused")
	private void  keepthrlastColum(HSSFSheet sheet,Integer moveto){
		
		SSFUtilColumn huc=new SSFUtilColumn(sheet,lastLines);
		
		List<HSSFCell> cells=huc.getCellList();
	
		for (int i = 0; i < cells.size(); i++) {
			
			HSSFCell cell=sheet.getRow(i).createCell(moveto);
			
			Object value=getValue(cells.get(i), hfe, formula);
			
			if(value!=null)setValue(cell,value, formula);
			
			cells.get(i).setCellType(3);
			
			setValue(cells.get(i),"", formula);
		}			
	}
	@SuppressWarnings("rawtypes")
	public Workbook bean2xls(Workbook workbook, String sheetname,
			List<T> list) throws Exception {

		Sheet sh = workbook.getSheet(sheetname);
		
		  if(sh==null){
			  
			 sh=workbook.createSheet(sheetname);
			  
		  }
		
		if (hfe == null) {
        	if(type.equals(SSFType.HSSF)) hfe = new  HSSFFormulaEvaluator((HSSFWorkbook)workbook);  
			
			if(type.equals(SSFType.XSSF)) hfe = new  XSSFFormulaEvaluator((XSSFWorkbook)workbook);
		}
		for (int i = 0; i < MatchFieldNames.length; i++) {
			
		
			
			if (MatchFieldNames[i] == null || MatchFieldNames[i].equals("")) {
				continue;
			}
			
			
			Row row = sh.getRow(i);
			
			if(row==null){
				
				row=sh.createRow(i);
				
			}
			
			if(ws.getRowHight(i)!=null){
			   row.setHeight(getWs().getRowHight(i));
			}
			
			for (int j = 0; j < end - start; j++) {

				if(i==1){
				
				 if(getWs().getColumHight(i)!=null){
				   sh.setColumnWidth(j, getWs().getColumHight(i));	
				   }
				}
				
				
				
				T intance = list.get(j);

				Field f = clazz.getDeclaredField(MatchFieldNames[i]);

				Class type = f.getType();

				String getMethodname = "get"
						+ MatchFieldNames[i].substring(0, 1).toUpperCase()
						+ MatchFieldNames[i].substring(1);

				Method method = clazz.getMethod(getMethodname);

				Cell cell = row.getCell(j + start);
				
				if(cell==null){
					
					cell=row.createCell(j+start);
					
				}
				
				
				setCellStyle(i, j+start, cell, workbook);
				
				Object value = null;

				if (Integer.class.equals(type)) {
					value = method.invoke(intance);
				}

				if (String.class.equals(type)) {
					value = method.invoke(intance);
				}

				if (Double.class.equals(type)) {

					value = method.invoke(intance);
				}

				if (value != null) {
				
					setValue(cell, value, formula);
					
					
					
				}
			}
		}
		return workbook;
	}




}
